# 25_utas2014.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-3-14 Taka-aki Asano
# Last Modified: 2021-10-10

# package
require("ltm")
require("plink")
require("readr")
require("dplyr")
require("tidyr")
require("psych")


# Voters
## read dataset
voter2014 <- read_csv(
  "http://www.masaki.j.u-tokyo.ac.jp/utas/2014_2016UTASV20161004.csv", 
  locale = locale(encoding = "CP932")
)

## specify variables
variables2014 <- c("W1Q14_1", "W1Q16_1", "W1Q16_2", "W1Q16_3", "W1Q16_4", 
                   "W1Q16_5", "W1Q16_6", "W1Q16_7", "W1Q16_8", "W1Q16_9", 
                   "W1Q16_10", "W1Q16_11", "W1Q16_12", "W1Q16_13", "W1Q16_14", 
                   "W1Q16_15", "W1Q16_16", "W1Q16_17", "W1Q17_1", "W1Q17_2", 
                   "W1Q17_3", "W1Q17_4", "W1Q17_5", "W1Q17_6", "W1Q17_7", 
                   "W1Q17_8", "W1Q17_9", "W1Q17_10")

## handle missing data
for(i in variables2014) {
  n <- voter2014[,i] == 66 | voter2014[,i] == 99
  voter2014[n, i] <- NA
}
voter2014 <- voter2014[rowSums(voter2014[,variables2014], na.rm = TRUE) != 0,]

## estimate item parameters
irt2014_voter <- ltm::grm(voter2014[,variables2014], IRT.param = FALSE)
irt2014_voter ## viewing

## estimate voters ideology
score2014 <- ltm::factor.scores(irt2014_voter, voter2014[,variables2014])


# rescale
## common item
common2009 <- c("Q013801", "Q013802", "Q013804", "Q013806", "Q013809", 
                "Q013811", "Q013812", "Q013816", "Q013817", "Q013818")
common2014 <- c("W1Q14_1", "W1Q16_1", "W1Q16_2", "W1Q16_3",  "W1Q16_5", 
                "W1Q16_6", "W1Q16_7", "W1Q16_12", "W1Q16_13", "W1Q16_11")
index <- data.frame(
  group1 = match(common2009, colnames(voter2009[,variables2009])), 
  group2 = match(common2014, colnames(voter2014[,variables2014]))
)
index

## list of parameters
### 2009
res2009 <- coef(irt2009_voter)
res2009 <- res2009[,c(5,1:4)]
colnames(res2009) <- c("a","b1","b2","b3","b4")
### 2014
res2014 <- coef(irt2014_voter)
res2014 <- res2014[,c(5,1:4)]
colnames(res2014) <- c("a","b1","b2","b3","b4")
### merge
pm <- list(res2009, res2014)

## list of scale
rescat <- list(rep(5, nrow(res2009)), 
               rep(5, nrow(res2014)))

## as.irt.pars
pm2009 <- as.poly.mod(n = nrow(res2009), model = "grm", 
                      items = 1:nrow(res2009))
pm2014 <- as.poly.mod(n = nrow(res2014), model = "grm", 
                      items = 1:nrow(res2014))
res <- as.irt.pars(pm, common = index, cat = rescat, 
                   poly.mod = list(pm2009, pm2014), 
                   location = FALSE)

## list of voters' position
ideology <- list(score2009$score.dat$z1, score2014$score.dat$z1)

## rescale
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
summary(plink_out)

## estimate voters ideology (rescale)
score2014_plink <- link.ability(plink_out)$group2
voter2014$Ideology <- -1 * score2014_plink

## Median
median(voter2014$Ideology)
by(voter2014$Ideology, voter2014$W1Q2, describe)
